
import { createRouter, createWebHistory } from 'vue-router'

// --- 统一导入所有需要的组件 ---

// 导入前台组件
import Home from '@/views/user/Home.vue'; 

// 导入后台组件
import AdminLogin from '@/views/admin/AdminLogin.vue'
import AdminRegister from '@/views/admin/AdminRegister.vue'
import AdminIndex from '@/views/admin/AdminIndex.vue'

// --- 将所有路由规则合并到一个数组中 ---
const routes = [
  // --- 前台路由 ---
  {
    // 规则1: 当用户访问根目录时，自动跳转到商城首页
    path: '/',
    redirect: '/home'
  },
  {
    // 规则2: 商城首页的路由
    path: '/home',
    name: 'Home',
    component: Home, 
    meta: {
      title: '商城首页'
    }
  },

  // --- 后台路由 ---
  {
    // 规则3: 管理员登录页
    path: '/admin/login',
    name: 'AdminLogin',
    component: AdminLogin,
    meta: {
      title: '管理员登录'
    }
  },
  {
    // 规则4: 管理员注册页 (如果需要的话)
    path: '/admin/register',
    name: 'AdminRegister',
    component: AdminRegister,
    meta: {
      title: '管理员注册'
    }
  },
  {
    // 规则5: 后台管理系统主布局
    // 所有后台子页面都会被渲染到 AdminIndex.vue 的 <router-view> 中
    path: '/admin',
    name: 'AdminIndex',
    component: AdminIndex,
    redirect: '/admin/dashboard', // 访问/admin时，默认显示dashboard
    meta: {
      title: '管理后台'
    },
    children: [
      {
        path: 'dashboard', 
        name: 'Dashboard',
        component: () => import('@/views/admin/DashBoard.vue'),
        meta: { title: '系统首页' }
      },
      {
        path: 'members/list',
        name: 'MemberList',
        component: () => import('@/views/admin/MemberManagement.vue'),
        meta: { title: '会员列表' }
      },
      {
        path: 'category/list',
        name: 'CategoryList',
        component: () => import('@/views/admin/AdminCategory.vue'),
        meta: { title: '种类列表' }
      },
      {
        path: 'orders/list',
        name: 'OrderList',
        component: () => import('@/views/admin/AdminOrder.vue'),
        meta: { title: '订单列表' }
      },
      {
        path: 'products/list',
        name: 'ProductList',
        component: () => import('@/views/admin/AdminProduct.vue'),
        meta: { title: '商品列表' }
      },

    ]
  }
];

// --- 创建并导出 Router 实例 ---
const router = createRouter({
  history: createWebHistory(),
  routes // 使用上面合并后的路由数组
});

export default router;
